﻿/*
 * @Author: ws.s 
 * @Date: 2021-05-31 10:12:07 
 * @Last Modified by: ws.s
 * @Last Modified time: 2021-05-31 10:23:05
 */
using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using System.Text;
using UnityEngine;
using WssUtils;

/// <summary>
/// 序列化工具，用于拷贝、复制数据、序列化数据等操作
/// v1.0.1 2021-05-31 10:12:07  修复，Load2DFromString,  unityAction == null条件反转问题
/// </summary>
public class SerializeTool
{

    public delegate T ConvertToObjectDelegate<T>(string str);
    public delegate string ConvertToStringDelegate<T>(T arg);

    private static TOut CloneObject<TIn, TOut>(TIn tIn)
    {
        TOut tOut = Activator.CreateInstance<TOut>();
        var tInType = tIn.GetType();
        foreach (var itemOut in tOut.GetType().GetProperties())
        {
            var itemIn = tInType.GetProperty(itemOut.Name); ;
            if (itemIn != null)
            {
                itemOut.SetValue(tOut, itemIn.GetValue(tIn));
            }
        }
        return tOut;
    }

    public static void CopyObjectFields(object input, object output)
    {
        if (input == null || output == null) return;
        var tInType = input.GetType();
        foreach (var itemOut in output.GetType().GetFields(BindingFlags.Instance | BindingFlags.Public))
        {
            var itemIn = tInType.GetField(itemOut.Name);
            if (itemIn != null)
            {
                itemOut.SetValue(output, itemIn.GetValue(input));
            }
        }
    }


    public static T[][] Load2DFromString<T>(string array2dStr, ConvertToObjectDelegate<T> unityAction = null, Char split0 = ',', Char split1 = ';')
    {
        string[][] list = array2dStr.Split2DArray(split0, split1);
        T[][] result = new T[][] { };
        Array.Resize(ref result, list.Length);
        for (int i = 0; i < result.Length; i++)
        {
            result[i] = new T[] { };
            Array.Resize(ref result[i], list[i].Length);
            for (int j = 0; j < result[i].Length; j++)
            {
                if (unityAction == null)
                {
                    result[i][j] = default(T);
                }
                else
                {
                    result[i][j] = unityAction.Invoke(list[i][j]);
                }
            }
        }
        return result;
    }

    public static string Save2DToString<T>(T[][] array2d, ConvertToStringDelegate<T> unityAction = null, Char split0 = ',', Char split1 = ';')
    {
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < array2d.Length; i++)
        {
            for (int j = 0; j < array2d[i].Length; j++)
            {
                if (unityAction != null)
                {
                    stringBuilder.Append(unityAction.Invoke(array2d[i][j]));
                }
                else
                {
                    stringBuilder.Append(array2d[i][j].ToString());
                }

                if (j != array2d[i].Length - 1)
                {
                    stringBuilder.Append(split0);
                }
            }
            if (i != array2d.Length - 1)
            {
                stringBuilder.Append(split1);
            }
        }
        return stringBuilder.ToString();
    }
}
